﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Resources;

public partial class admin_LocationList : SitePage
{
    busLocation _busLocation = new busLocation();
    #region ViewState
    public int count = 0;




    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            LoadData();
        }
        //  Response.Write("Application:" + this.ApplicationName);
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    #endregion

    #region Events

    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Location objData = (Location)e.Item.DataItem;
            if (objData == null)
                return;
            hidID.Value += objData.LocationId.ToString() + ";";

            //image button IsActive
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbActive");
            imgButton.CommandArgument = objData.LocationId.ToString();
            if (objData.IsActive == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeActive";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Active";
            }
            //image button Order
            imgButton = (ImageButton)e.Item.FindControl("imbOrderDown");
            if (e.Item.ItemIndex + 1 == count)
                imgButton.Visible = false;
            else
            {
                imgButton.ToolTip = Resources.Resource.TIP_MOVEDOWN;
                imgButton.CommandArgument = objData.LocationId.ToString();
                imgButton.ImageUrl = clsString.ORDERDOWN_IMAGE_URL;
            }
            imgButton = (ImageButton)e.Item.FindControl("imbOrderUp");
            if (e.Item.ItemIndex == 0)
                imgButton.Visible = false;
            else
            {
                imgButton.CommandArgument = objData.LocationId.ToString();
                imgButton.ToolTip = Resources.Resource.TIP_MOVEUP;
                imgButton.ImageUrl = clsString.ORDERUP_IMAGE_URL; ;
                imgButton.Visible = true;
            }

            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.LocationId.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
            System.Web.UI.HtmlControls.HtmlTableRow row = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");
            if (e.Item.ItemIndex % 2 == 0)
                row.Attributes.Add("class", "alt-row");

        }

    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        int id = clsConvert.Int(e.CommandArgument.ToString());
        switch (e.CommandName)
        {

            case "OrderUp":
                {
                    OrderUp(id);
                    break;
                }
            case "OrderDown":
                {
                    OrderDown(id);
                    break;
                }
            case "Active":
                {
                    Active(id);
                    break;
                }
            case "DeActive":
                {
                    DeActive(id);
                    break;
                }

            case "Delete":
                {
                    if (Delete(id))
                        LoadData();
                    break;
                }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {
                Delete(clsConvert.Int(arr[i].ToString()));
            }
        }
        LoadData();

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("LocationDetail.aspx");
    }

    #endregion

    #region Methods


    private void OrderDown(int id)
    {
        if (_busLocation.OrderDown(id))
            LoadData();
    }
    private void OrderUp(int id)
    {
        if (_busLocation.OrderUp(id))
            LoadData();
    }
    private void Active(int id)
    {

        if (_busLocation.Active(id))
            LoadData();
    }
    private void DeActive(int id)
    {
        if (_busLocation.DeActive(id))
            LoadData();
    }
    private bool Delete(int id)
    {
        //get Image

        //Delete Data
        return _busLocation.Delete(id);
    }

    private void LoadData()
    {

        hidID.Value = string.Empty;

        LocationCollection lstResult = _busLocation.List();
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
        }

        rptData.DataSource = lstResult;
        rptData.DataBind();

    }

    #endregion
}